<!DOCTYPE html>
<html>

    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <title>登录</title>
        <link rel="stylesheet" href="/static/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/static/admin/css/other/login.css" />
        <link rel="stylesheet" href="/static/admin/css/variables.css" />
        <script>if (window.self != window.top) { top.location.reload(); }</script>
    </head>

    <body>
        <div class="login-page" style="background-image: url(/static/admin/images/background.svg)">
            <div class="layui-row">
                <div class="layui-col-sm6 login-bg layui-hide-xs">
                    <img class="login-bg-img" src="/static/admin/images/banner.jpg" alt="" />
                </div>
                <div class="layui-col-sm6 layui-col-xs12 login-form">
                    <div class="layui-form">
                        <div class="form-center">
                            <div class="form-center-box">
                                <div class="top-log-title">
                                    <img class="top-log" src="/static/admin/images/logo.png"
                                        alt="" />
                                    <span>后台管理系统</span>
                                </div>
                                <div style="margin-top: 30px;">
                                    <div class="layui-form-item">
                                        <div class="layui-input-wrap">
                                            <div class="layui-input-prefix">
                                                <i class="layui-icon layui-icon-username"></i>
                                            </div>
                                            <input id="username" name="username" lay-verify="required" placeholder="用户名"
                                                autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <div class="layui-input-wrap">
                                            <div class="layui-input-prefix">
                                                <i class="layui-icon layui-icon-password"></i>
                                            </div>
                                            <input id="password" name="password" type="password" value=""
                                                lay-verify="required|confirmPassword" placeholder="密码"
                                                autocomplete="off" class="layui-input" lay-affix="eye">
                                        </div>
                                    </div>

                                    <div class="login-btn">
                                        <button id="login-btn" type="button" lay-submit lay-filter="login"
                                            class="layui-btn login">
                                            登录
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- 资 源 引 入 -->
        <script src="/static/admin/component/layui/layui.js"></script>
        <script src="/static/admin/component/pear/pear.js"></script>
        <script type="text/javascript">
            layui.use(['form', 'button', 'popup'], function () {
                var form = layui.form;
                var button = layui.button;
                var popup = layui.popup;

                // 登 录 提 交
                form.on('submit(login)', function (data) {
                    // 验证
                    document.getElementById("login-btn").disabled = true;   // 禁用按钮
                    // console.log("表单提交提交");
                    // 登录
                    // post 请求
                    fetch("/admin/login", {
                        method: "POST",
                        headers: {
                            "Accept": "application/json",
                            "Content-Type": "application/json;charset=utf-8"
                        },
                        body: JSON.stringify(data.field)
                    })
                        .then(res => res.json())
                        .then(res => {
                            // console.log(res);
                            if (res.code != 200) {
                                document.getElementById("login-btn").disabled = false;   // 启用按钮
                                return popup.error(res.msg);
                            }

                            sessionStorage.setItem("username", res.data.username);
                            sessionStorage.setItem("nickname", res.data.nickname);
                            sessionStorage.setItem("token", res.data.token);

                            // 动画
                            button.load({
                                elem: '.login',
                                time: 800,
                                done: function () {
                                    popup.success("登录成功", function () {
                                        location.href = "/admin/index";
                                    });
                                }
                            });
                        })
                        .catch((error) => {
                            console.error("Error:", error);
                        });


                    return false;
                });


            });
        </script>
    </body>

</html>